package DiannaoShiyongXianzhi;

import java.io.*;
import java.util.Scanner;
import java.awt.Container;
import java.awt.Frame;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.AbstractMultiResolutionImage;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JTextField;


public class SheZhi implements ActionListener {
	//private static final long serialVersionUID = 1L;
	private JFrame frame;
	private JButton baocun;  //保存
    private JLabel label;  //使用开始时间
    private JLabel label2;  //使用结束时间
    private JLabel label3;  //使用时长秒
    private JLabel label4;  //备用
    private JTextField textField;
    private JTextField textField2;
    private JTextField textField3;
    private JTextField textField4;
    public float[] shezhicanshu = {(float) 14.01, (float) 22.01, 3600, 888, 888, 888};
    public String mima = "123456";
    private String[] shezhicanshu2 = {"14.01", "22.01", "3600", "888", "888", "888"};
    
    
    public SheZhi() {
    	frame = new JFrame("设置");
    	baocun = new JButton("保存");
    	label = new JLabel("开始使用时间：");
    	label2 = new JLabel("结束使用时间：");
    	label3 = new JLabel("允许使用时长(秒)：");
    	label4 = new JLabel("修改密码：");
    	duquWenjian();  //读取设置参数
    	textField = new JTextField();
    	textField.setText(shezhicanshu2[0]);
    	textField2 = new JTextField();
    	textField2.setText(shezhicanshu2[1]);
    	textField3 = new JTextField();  //秒
    	textField3.setText(shezhicanshu2[2]);
    	textField4 = new JTextField();
    	textField4.setText(mima);
    	Container cc = new Container();
    	
    	baocun.addActionListener(this);  //注册监听程序
    	
    	frame.setLayout(new GridLayout(5,2));  //添加网格布局
    	frame.add(label);
    	frame.add(textField);
    	frame.add(label2);
    	frame.add(textField2);
    	frame.add(label3);
    	frame.add(textField3);
    	frame.add(label4);
    	frame.add(textField4);
    	frame.add(cc);
    	frame.add(baocun);
    	//显示窗口
    	// 窗口关闭事件监听器
    	//WindowAdapter.addWindowListener(this);
    	
    	frame.setSize(230,150);
    	frame.setLocationRelativeTo(null);  //打开窗口就居中显示
    	frame.setResizable(false);  //取消最大化按钮
    	//pack();  //启动就最大化
    	//frame.setVisible(true);
    	frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    	//frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    	
    }
    
    //读取
    void duquWenjian() {
    	try {
    	    FileInputStream fileInputStream = new FileInputStream("text.sjxz");
    	    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
    	    String line;
    	    int ii = 0;
    	    while ((line = bufferedReader.readLine()) != null) {
    	    	shezhicanshu2[ii]= line;
    	    	Float lsblFloat = Float.valueOf(line);
    	    	shezhicanshu[ii]= lsblFloat;
    	    	System.out.println(shezhicanshu[ii]);
    	    	//System.out.println(line);
    	    	ii++;
    	    }
    	    mima = shezhicanshu2[3];
    	    bufferedReader.close();
    	    fileInputStream.close();
    	    
    	} catch (IOException e) {
    	    e.printStackTrace();
    	}
    }
    
    
    //保存
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == baocun) {  //为保存按钮事件
        	
            try (FileOutputStream  out = new FileOutputStream("text.sjxz")) {
            	
            	String data = textField.getText()+"\n"+textField2.getText()+"\n"+textField3.getText()+"\n"+textField4.getText();
                byte[] bytes = data.getBytes();
            	out.write(bytes);
    	    	Float lsblFloat = Float.valueOf(textField.getText());
    	    	shezhicanshu[0]= lsblFloat;
    	    	System.out.println(shezhicanshu[0]);
    	    	lsblFloat = Float.valueOf(textField2.getText());
    	    	shezhicanshu[1]= lsblFloat;
    	    	System.out.println(shezhicanshu[1]);
    	    	lsblFloat = Float.valueOf(textField3.getText());
    	    	shezhicanshu[2]= lsblFloat;
    	    	System.out.println(shezhicanshu[2]);
    	    	mima = textField4.getText();
    	    	//System.out.println(line);
            	
            	

            } catch (IOException ex) {
                ex.printStackTrace();
            }
        	
           System.out.println("你好，已保存");

        }
    }

    //打开窗口
    public void daKai() {
    	frame.setVisible(true);   
    	frame.setAlwaysOnTop(true); // 设置窗口为置顶窗口
    }
    /*
    public static void main(String[] args) {
    	SheZhi sheZhi = new SheZhi();
    	//System exit(0);
    	
    	
    }
*/
}




















